package com.ytjj.qmyx.supplychain.service;

import com.ytjj.qmyx.supplychain.mapper.BankerProductSpecMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerProductSpec;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductSpec;
import com.ytjj.qmyx.supplychain.mapper.ProductSpecMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProductSpecService {
    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource
    private BankerProductSpecMapper bankerProductSpecMapper;

    /**
     * 更新商品规格库存
     * @param specId 规格id
     * @param num 更新数量
     * @param type 更新类型 sub-扣减 add-增加
     * @return
     */
    public boolean updateProductSpecStock(Integer specId, Integer num, String type) {
        YxProductSpec spec = productSpecMapper.selectByPrimaryKey(specId);
        if (null == spec) {
            log.error("===updateProductSpecStock is fail by productSpec is null==={}, {}", specId, type);
            return false;
        }
        if ("sub".equals(type)) {
            spec.setStock(spec.getStock() - num);
            spec.setBuySum(spec.getBuySum() + num);
        }else {
            spec.setStock(spec.getStock() + num);
        }
        if (spec.getStock().intValue() <= 0) {
            spec.setStock(0);
        }
        int result = productSpecMapper.updateByPrimaryKeySelective(spec);

        //同步修改供应商规格库存
        YxBankerProductSpec yxBankerProductSpec = bankerProductSpecMapper.selectByPrimaryKey(spec.getSupplyId());
        int bankerResult = 0;
        if (null != yxBankerProductSpec) {
            yxBankerProductSpec.setStock(spec.getStock());
            bankerResult = bankerProductSpecMapper.updateByPrimaryKeySelective(yxBankerProductSpec);
        }
        return result > 0 ? true : false;
    }

    /**
     * 根据供应链商品id查询供应链商品的规格信息
     * @param productIds
     * @return
     */
    public List<YxProductSpec> selectProductSpecByProductIds(List<Integer> productIds) {
        List<YxProductSpec> productSpecList = new ArrayList<>();
        int forNum = productIds.size() / 200;
        int mord = productIds.size() % 200;
        if (mord > 0) {
            forNum = forNum + 1;
        }
        for (int i = 0; i < forNum; i++) {
            List<Integer> selectProductIds = new ArrayList<>();
            if (i * 200 + 200 > productIds.size()) {
                selectProductIds = productIds.subList(i * 200, i * 200 + 200);
            }else {
                selectProductIds = productIds.subList(i * 200, productIds.size());
            }
            Example example = new Example(YxProductSpec.class);
            example.createCriteria().andIn("productId", selectProductIds);
            example.selectProperties("id", "stock");
            productSpecList.addAll(productSpecMapper.selectByExample(example));
        }

        return productSpecList;
    }
}
